<?php
	if ($_SERVER['SERVER_ADDR'] != $_SERVER['REMOTE_ADDR']){
		die("Access denied!");
	}

require_once "wp-config.php";
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_select_db(DB_NAME);
mysql_query("SET NAMES utf8"); //connect in decode utf8
date_default_timezone_set('Asia/Ho_Chi_Minh');
$sjchcm = getlastprice('SJCHCM');
		$title = "Giá vàng hiện tại ".$sjchcm['gia_mua']."K/".$sjchcm['gia_ban']."K";
		$slug=make_post_slug("gia-vang-hien-tai-".$sjchcm['gia_mua']."-".$sjchcm['gia_ban']."-".date("Y-m-d"));
		//$content = "<div>Giá vàng tại thời điểm này là </div>";
        $content = file_get_contents("http://vip.giavang.net/data/GoldenPrice.php");
		$content=str_replace("Cung cấp bởi Giavang.net","http://baogia.info/",$content);
		$content=str_replace("Cung cấp bởi WWW.GIAVANG.NET","http://baogia.info/",$content);
		$content=str_replace("http://www.giavang.net","http://baogia.info/",$content);
		$content=str_replace("#ffffff","#ff0000",$content);
		$content=str_replace("#ffff00","#222222",$content);
		$content.="<div id='baogiainfo'> <a href='http://baogia.info'>Báo giá vàng</a> | <a href='http://baogia.info'>http://baogia.info</a></div>";
		$content.="<div id='xemthem'>Xem thêm: <ul><li><a href='http://baogia.info/bien-dong/'>Biến động giá vàng trong ngày</a></li></ul></div>";
		$post_id=insert_post($title,$content,$slug);
		if ($post_id) {
			add_post_category($post_id,"Giá vàng");
			add_post_category($post_id,"Báo giá");
			add_post_tag($post_id,"Giá vàng");
			//add_post_tag($post_id,"Thị trường");
			add_post_tag($post_id,"Bản tin");
			add1_post_meta($post_id,"rating","5");
            add1_post_meta($post_id,"votes","1");
			
		}
	echo $post_id;//."\n".$slug;

function getlastprice($mahang) {
		$sql="select * from g_banggia where gia_mahang='$mahang' order by gia_time desc limit 1";
		$ret = mysql_query($sql);
		//if ($data=mysql_fetch_array($ret)) return $data;
		//else return NULL;
		return mysql_fetch_array($ret);
	}
	function pGet($p) {
		if (isset($_GET[$p])) return $_GET[$p];
		else return "";
	}
function addListCat($pid,$bcat) {
	if ($bcat!="") {
				$c = split(";",$bcat);
				if (count($c)>0) {
					for ($i=0; $i<count($c); $i++) {
						if (strlen($c[$i])>0)
						add_post_listcat($pid,trim($c[$i]));
					}
				}
			}
}
function gPost($gp)
{
	if (isset($_POST[$gp]))
		return $_POST[$gp];
	else
		return "";
}

function insert_post($title,$content,$slug='') {
	$today = date("Y-m-d H:i:s");
	$sql="INSERT INTO `wp_posts` (`post_author`,`post_date`,`post_date_gmt`,`post_content`,`post_title`,`post_excerpt`,`post_status`,`comment_status`,`ping_status`,`post_password`,`post_name`,`to_ping`,`pinged`,`post_modified`,`post_modified_gmt`,`post_content_filtered`,`post_parent`,`guid`,`menu_order`,`post_type`,`post_mime_type`,`comment_count`) VALUES ('1','$today','$today','".mysql_real_escape_string($content)."','$title','','draft','open','open','','$slug','','','$today','$today','','0','http://baogia.info/$slug','0','post','','0');";
	//echo $sql;
	$post_id=0;
	$result = mysql_query($sql);
	/**/
	if ($result) {
		$post_id= mysql_insert_id();
	}
	/**/
	return $post_id;
}
function insert_term($term_name,$term_type,$slug='') { //$term_type = category or post_tag
		$sql="select * from `wp_terms` where `name`='$term_name'";
		$result = mysql_query($sql);
		if (mysql_num_rows($result)) {
			//echo $sql;
			$data=mysql_fetch_array($result);
			$cat_id=$data['term_id'];
		} else {
			$sql="INSERT INTO `wp_terms` (`name`,`slug`,`term_group`) VALUES ('$term_name','$slug','0');";
			$result = mysql_query($sql);
			$cat_id= mysql_insert_id();
			//echo $sql;
		}
		$sql="select * from `wp_term_taxonomy` where `term_id`='$cat_id' and `taxonomy`='$term_type'";
		$result = mysql_query($sql);
		if (mysql_num_rows($result)) {
			$data=mysql_fetch_array($result);
			$tt_id=$data['term_taxonomy_id'];
		} else {
			$sql="INSERT INTO `wp_term_taxonomy` (`term_id`,`taxonomy`,`description`,`parent`,`count`) VALUES ('$cat_id','$term_type','','0','1');";
			$result = mysql_query($sql);
			$tt_id= mysql_insert_id();
		}
		//echo "tt$tt_id<br/>";
		return  $tt_id;
}

function add_term_to_post($post_id,$term_name,$term_type) {
	//$slug=str_replace(" ","-",$term_name);
	$slug=make_cat_slug($term_name);
	$term_id=insert_term($term_name,$term_type,$slug);
	//echo "tt: $term_id<br/>";
	if ($term_id) {
		$sql="INSERT IGNORE INTO `wp_term_relationships` (`object_id`,`term_taxonomy_id`,`term_order`) VALUES ('$post_id','$term_id','0');";
		$result = mysql_query($sql);
		return mysql_insert_id();
	}
	return 0;
}

function add_post_category($post_id,$cat_name) {
	return add_term_to_post($post_id,$cat_name,'category');
}
function add_post_listcat($post_id,$cat_name) {
	return add_term_to_post($post_id,$cat_name,'listcat');
}
function add_post_tag($post_id,$tag_name) {
	return add_term_to_post($post_id,$tag_name,'post_tag');
}
function add1_post_meta($post_id,$meta, $txt) {
	$sql="INSERT INTO wp_postmeta (post_id,meta_key,meta_value) VALUES('$post_id','$meta','$txt');";
	$result = mysql_query($sql);
}

//function sanitize_title_with_dashes($title) { 
//	$url_pattern = array('`&(amp;|#)?[a-z0-9]+;`i', '`[^a-z0-9]`i'); 
//	$title = htmlentities($title, ENT_COMPAT, 'utf-8'); 
//	$title = preg_replace( '`&([a-z]+)(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig);`i', "\1", $title ); 
//	$title = preg_replace('`[.*]`U','',$title); 
//	$title = strtolower(trim($title, '-')); 
//	$title = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $title); 
//	$title = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $title); 
//	$title = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $title); 
//	$title = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $title); 
//	$title = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $title); 
//	$title = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $title); 
//	$title = preg_replace("/(đ)/", 'd', $title); 
//	$title = preg_replace($url_pattern , '-', $title);
//	$title = preg_replace('---' , '-', $title);
//	$title = preg_replace('--' , '-', $title);	
//	return $title; 
//}


function slugify($str, $delimiter='-') {
	$clean = $str;
	if (function_exists('iconv'))
	$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $clean);
	$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
	$clean = strtolower(trim($clean, '-'));
	$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
	return $clean;
}
function make_post_slug($bname) {
	//$slug = slugify($bname).date("Y-m-d");
	$slug = $bname;
	$noi="-";
	while (true) {
		$sql = "select post_name from wp_posts where post_name='$slug' limit 1";
		$result = mysql_query($sql);
		if (mysql_num_rows($result)) {
			$slug.=$noi.chr(rand(48,57));
			$noi="";
		} else {
			break;
		}
	}
	return $slug;
}
function make_cat_slug($bname) {
	$slug = slugify($bname);
	return $slug;
}
?>
